package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.mk.cloud.common.domain.PCoconfig;
import com.mk.cloud.common.domain.PCompany;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.util.DateUtils;
import com.mk.cloud.pt.service.app.PCompanyService;
import org.osgl.util.S;import com.mk.cloud.common.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.With;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("company")
public class PCompanyController extends CookieService {

    @Inject
    private PCompany.Mapper pCompnayMapper;
    @Inject
    private PCoconfig.Mapper pCoconfigMapper;
    @Inject
    private PCompanyService pCompnayService;


    @Description("企业--企业分页列表")
    @With(ICLogin.class)
    @GetAction("pageList.json")
    public PageQuery<PCompany> pCompanyList(
            @Description("筛选条件-企业名称") String coname,
            @Description("当前第几页") @NotNull Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize

    ) {
        PageQuery<PCompany> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> map = new HashMap<>();
        map.put("coname", coname);
        map.put("coid", me.getCoid());
        query.setParas(map);
        pCompnayMapper.getSQLManager().pageQuery("pCompany.select_company_page_list", PCompany.class, query);
        return query;
    }

    @Description("企业--企业列表")
    @With(ICLogin.class)
    @GetAction("list.json")
    public List<PCompany> pCompanyListAll(
    ) {
        List<PCompany> list = pCompnayMapper.all();
        return list;
    }


    @Description("企业--企业详情")
    @With(ICLogin.class)
    @GetAction("get.json")
    public PCompany getPCompany(@Description("企业id") String coid) {
        if (S.isEmpty(coid)) {
            return new PCompany();
        }
        return pCompnayMapper.single(coid);
    }

    @Description("企业--企业保存")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("save.json")
    public String savePCompany(@Description("企业对象") PCompany company) {
        if (S.isNotBlank(company.getCoid())) {
            pCompnayMapper.updateTemplateById(company);
            return company.getCoid();
        } else {
            String coid = S2.UUID();
            company.setCoid(coid);
            company.setCreatetime(DateUtils.getTimeNow());
            pCompnayMapper.insert(company);
            return coid;
        }
    }

    @Description("企业--企业配置")
    @With({ICLogin.class, ITransacte.class})
    @GetAction("config.json")
    public PCoconfig config(@Description("企业配置") String coid) {
        PCoconfig coconfig = pCoconfigMapper.single(coid);

        PCoconfig pCoconfig= new PCoconfig();
        pCoconfig.setCoid(coconfig.getCoid());
        if(pCoconfig!=null){
            pCoconfig.setBegintime(coconfig.getBegintime());
            pCoconfig.setEndtime(coconfig.getEndtime());
        }
        return pCoconfig;
    }

    @Description("企业--企业配置保存")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("configSave.json")
    public PCoconfig configSave(@Description("企业配置") PCoconfig coconfig) {
        PCoconfig pCoconfig = pCoconfigMapper.single(coconfig.getCoid());
        if(pCoconfig==null){
            pCoconfigMapper.insert(coconfig);
        }else {
            pCoconfigMapper.updateTemplateById(coconfig);
        }
        pCompnayService.clearCashByCoid(coconfig.getCoid());
        return coconfig;
    }




}
